package com.xiaoshangkou.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.xiaoshangkou.myapplication.commons.Permission;
import com.xiaoshangkou.myapplication.recording.ScreenRecordService;
import com.xiaoshangkou.myapplication.thread.SubThread_TT;
import com.xiaoshangkou.myapplication.thread.SubThread_TT2;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_CODE = 1;
    private Thread thread;
    private MediaProjectionManager mMediaProjectionManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
        Button button=findViewById(R.id.stopbutton1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent service = new Intent(MainActivity.this, ScreenRecordService.class);
                stopService(service);
                thread.interrupt();
                Toast.makeText(MainActivity.this, "停止录屏", Toast.LENGTH_SHORT).show();
            }
        });


    }

    //开始录屏
    public void StartRecorder(View view) {
        EditText editText = findViewById (R.id.editText1);
        String ip=editText.getText().toString();
        thread = new Thread(new SubThread_TT2(ip));
        thread.start();
        Intent captureIntent = mMediaProjectionManager.createScreenCaptureIntent();
        startActivityForResult(captureIntent, REQUEST_CODE);
    }

    //停止录屏
//    public void StopRecorder(View view){
//        System.out.println("你好");
//        Intent service = new Intent(this, ScreenRecordService.class);
//        stopService(service);
//        thread.stop();
//    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //如果权限满足
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            try {
                DisplayMetrics metric = new DisplayMetrics();
                MainActivity.this.getWindowManager().getDefaultDisplay().getRealMetrics(metric);

                int width = metric.widthPixels; // 宽度（PX）
                int height = metric.heightPixels; // 高度（PX）
                float density = metric.density; // 密度（0.75 / 1.0 / 1.5）
                int densityDpi = metric.densityDpi;
                Toast.makeText(this, "允许录屏", Toast.LENGTH_SHORT).show();

                Intent service = new Intent(this, ScreenRecordService.class);
                service.putExtra("resultCode", resultCode);
                service.putExtra("data", data);
                service.putExtra("width", width);
                service.putExtra("densityDpi", densityDpi);
                service.putExtra("height", height);
                service.setAction("android.intent.action.RESPOND_VIA_MESSAGE");
                startService(service);

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, "拒绝录屏", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    protected void onStart() {
        super.onStart();
        Permission.checkPermission(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }
}